from fastapi import APIRouter
from fastapi import Request
from service.DeptService import DeptService
from util.MyExceptionHandler import MyExceptionHandler
import inspect
import json
from util import Logger
from StatusCode import StatusCode

router = APIRouter(tags=["科室管理路由"])


@router.get("/dept/list")
def getDeptList(request: Request):
    try:
        json_data = dict(request.query_params)
        data = DeptService().getDeptList()
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records"),
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/dept/info")
def getDeptInfo(request: Request):
    try:
        json_data = dict(request.query_params)
        data = DeptService().getDeptInfo(json_data)
        if data.empty:
            response = {
                "code": StatusCode.RESPONSE_FAIL,
                "msg": "获取患者信息失败,请检查数据后重试",
            }
            return response
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records")[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/dept/update")
async def updateDept(request: Request):
    try:
        json_data = await request.json()
        flag = DeptService().updateDept(json_data)
        response = {"code": flag}
        if flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "修改科室信息失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/dept/add")
async def addDept(request: Request):
    try:
        json_data = await request.json()
        flag = DeptService().addDept(json_data)
        response = {"code": flag}
        if flag == StatusCode.RESPONSE_EXIST:
            response["msg"] = "新增科室失败,该科室已存在"
        elif flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "新增科室失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/dept/del")
def delDept(request: Request):
    try:
        json_data = dict(request.query_params)
        flag = DeptService().delDept(json_data)
        response = {"code": flag}
        if flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "删除科室失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/dept/changeState")
def changeDeptState(request: Request):
    try:
        json_data = dict(request.query_params)
        flag = DeptService().changeDeptState(json_data)
        response = {"code": flag}
        if flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "状态切换失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response
